package com.tzsp.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tzsp.reggie.commons.contans.R;
import com.tzsp.reggie.domain.Employee;
import com.tzsp.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
        //1、将页面提交的密码进行md5加密
        String password= employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2、根据页面提交的用户名查询数据库
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //添加查询条件，等值查询
        lqw.eq(Employee::getUsername,employee.getUsername());
        //将username给到service层 ，最后通过mapper在数据库中是否有username，
        Employee emp = employeeService.getOne(lqw);
        //3、如果没有查到则报错
        if (emp==null){
            return R.error("用户名或密码错误");
        }
        //4、比对密码，如果不一致则报错
        if (!emp.getPassword().equals(password)){
            return R.error("用户名或密码错误");
        }
        //5、查询员工状态，如果为禁用，则提示账号禁用
        if (emp.getStatus()==0){
            return R.error("账号被禁用，请联系管理员");
        }

        //6、登陆成功，将员工id存入到session，并且返回登陆成功接口
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);

    }

    /**
     * 登出操作
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){

        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 添加保存员工信息
     * @param employee
     * @param request
     * @return
     */
    @PostMapping
    public R<String> addSaveEmployee(@RequestBody Employee employee,HttpServletRequest request){
        log.info("employee收集到的参数是：{}",employee);
        Long id = Thread.currentThread().getId();
        log.info("线程id：{}",id);
        //收集参数，封装参数
        /*
        Long emplId= (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(emplId);
        employee.setUpdateUser(emplId);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        */
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);

        //调用service层进行数据处理
        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 分页查询员工信息，通过mp插件
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> queryEmployeeInfoByPage(int page,int pageSize,String name){
        log.info("page={},pageSize={},name={}",page,pageSize,name);

        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //添加过滤条件，
        lqw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        lqw.orderByDesc(Employee::getCreateTime);
        //执行 查询
        employeeService.page(pageInfo,lqw);

        return R.success(pageInfo);
    }

    /**
     * 根据id对象员工进行修改
     * 修改状态，修改其他基本信息
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> updataEmployeeInfo(HttpServletRequest request,@RequestBody Employee employee){
        /*Long emplId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(emplId);*/
        employeeService.updateById(employee);

        Long id = Thread.currentThread().getId();
        log.info("线程id：{}",id);
        return R.success("状态更新成功");
    }

    /**
     * 根据id获取员信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getEmployeeInfo(@PathVariable Long id){

        Employee employee = employeeService.getById(id);
        if (employee!=null){
            return R.success(employee);
        }
        return R.error("没有该员工信息");
    }

}
